<template>
  <a-table class="table-singe-line show-border-table" ref="tableListref" :span-method="spanMethodAll" row-key="id" :expandable="expandable" :default-expand-all-rows="true" :loading="tableLoading" :columns="tableColumns" :data="tableList" size="mini" :pagination="false" :scroll="{ x: 1600, y: '100%' }">
    <template #columns>
      <a-table-column v-for="(item, index) of tableColumns" :fixed="item.fixed" :key="index" :align="item.align" :width="item.width" :title="item.title" :data-index="item.dataIndex">
        <template v-if="item.tip" #title>
          <a-popover>
            <template #content>
              <p style="max-width: 400px">{{ item.tip }}</p>
            </template>
            <p>
              <span>{{ item.title }}</span>
              <icon-question-circle />
            </p>
          </a-popover>
        </template>

        <template v-if="item.dataIndex == 'img'" #cell="{ record }">
          <div class="order-w flex-c-sb" style="padding-right: 20px">
            <div class="flex">
              <div class="order-detail-item" style="width: 260px">
                <div class="flex-c">
                  <span class="label">交货单号：</span>
                  <span class="highlight">{{ record.billNo }}</span>
                  <CopyText class="popup-edit-icon-hover" style="margin-left: 5px" :val="record.billNo" />
                </div>
                <div class="flex-c">
                  <span class="label">收货仓库：</span>
                  <TextLimit style="flex: 1" :val="`${record.warehouseName}(${record.warehouseCode})`" :rows="1" />
                </div>
              </div>

              <div class="order-detail-item" style="width: 220px">
                <p><span class="label">创建人：</span>{{ record.createUname }}</p>
                <p><span class="label">提交时间：</span>{{ $setTime(record.gmtSubmit, 'YYYY/MM/DD HH:mm') }}</p>
              </div>
              <div class="order-detail-item" style="width: 220px">
                <p><span class="label">签收人：</span>{{ record.warehouseUname }}</p>
                <p><span class="label">签收时间：</span>{{ $setTime(record.gmtFinished, 'YYYY/MM/DD HH:mm') }}</p>
              </div>
              <div class="order-detail-item" style="width: 190px">
                <p><span class="label">交货数：</span>{{ record.deliveryAmt }}</p>
                <p><span class="label">实际交货数：</span>{{ record.deliveryRealAmt }}</p>
              </div>
            </div>
          </div>
        </template>
      </a-table-column>
    </template>
    <template #expand-row="{ record }">
      <a-table class="table-singe-line" row-class="child-row" :show-header="false" row-key="id" :bordered="false" :columns="tableColumns" :data="record.billItems" size="mini" :pagination="false">
        <template #columns>
          <a-table-column :width="10" />
          <a-table-column v-for="(item, index) of tableColumns" :key="index" :fixed="item.fixed" :align="item.align" :width="item.width" :title="item.title" :data-index="item.dataIndex">
            <template v-if="item.dataIndex == 'sort'" #cell> </template>
            <template v-else-if="item.dataIndex == 'img'" #cell="option">
              <ImgPopover :src="option.record.productImg">
                <template #con>
                  <a-image width="40" height="40" fit="contain" :src="option.record.productImg" />
                </template>
              </ImgPopover>
            </template>
            <template v-else-if="item.dataIndex == 'productName'" #cell="option">
              <TextLimit :val="option.record.productName" :rows="1" />
            </template>
            <template v-else-if="item.dataIndex == 'sku'" #cell="option">
              <p class="highlight cursor">{{ option.record.sku }}</p>
              <div class="urgent" v-show="option.record.urgent">急</div>
            </template>
            <template v-else-if="item.dataIndex == 'productSpecs'" #cell="option">
              <TextLimit :val="getProductSpecs(option.record)" />
            </template>
            <template v-else-if="item.dataIndex == 'remark'" #cell="option">
              <TextLimit :val="option.record.remark || '--'" :rows="2" />
            </template>
            <template v-else-if="item.dataIndex == 'gmtExpectDelivery'" #cell="option">
              <p>{{ $setTime(option.record.gmtExpectDelivery, 'YYYY/MM/DD') }}</p>
            </template>
            <template v-else-if="['materialPrice', 'productPrice', 'laborPrice'].includes(item.dataIndex)" #cell="option">
              <p>￥{{ option.record[item.dataIndex] }}</p>
            </template>
            <template v-else-if="item.dataIndex == 'rate'" #cell="option">
              <p>{{ option.record.rate }}%</p>
              <p class="highlight">{{ option.record.priceType == 1 ? '含税' : '不含税' }}</p>
            </template>
            <template v-else-if="item.dataIndex == 'planNo'" #cell="option">
              <p class="highlight">{{ option.record.planNo }}</p>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </template>
  </a-table>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { getDeliveryByPurchaseId } from '@/api/purchaseApplication'
  import { parseJson } from '@/utils'

  onMounted(() => {
    getTableList()
  })

  const props = defineProps({
    currentId: {
      type: String,
    },
  })
  const tableColumns: Array<any> = [
    {
      title: '图片',
      dataIndex: 'img',
      width: 60,
    },
    {
      title: '产品名称',
      dataIndex: 'productName',
      width: 100,
    },
    {
      title: 'SKU',
      dataIndex: 'sku',
      width: 100,
    },
    {
      title: 'SKU规格',
      dataIndex: 'productSpecs',
      width: 100,
    },
    {
      title: '交货数量',
      dataIndex: 'deliveryAmt',
      tip: '创建交货单时填写的数量',
      width: 85,
    },
    {
      title: '交货材质重量',
      dataIndex: 'materialWeight',
      tip: '金银等贵金属首饰类产品是按材质重量计算价格的，所以交货时必须要有材质重量',
      width: 80,
    },
    {
      title: '实际签收数量',
      dataIndex: 'deliveryRealAmt',
      tip: '交货单签收的实际数量',
      width: 80,
    },
    {
      title: '实收材质重量',
      dataIndex: 'materialRealWeight',
      tip: '实收材质重量=(交货材质重量/交货数量)*实际签收数量',
      width: 80,
    },

    {
      title: '材质单价(元/g)',
      dataIndex: 'materialPrice',
      width: 80,
    },
    {
      title: '产品单价',
      dataIndex: 'productPrice',
      width: 70,
    },
    {
      title: '工费单价',
      dataIndex: 'laborPrice',
      width: 70,
    },
    {
      title: '税率',
      dataIndex: 'rate',
      width: 60,
    },

    {
      title: '实收材质金额',
      dataIndex: 'materialAmount',
      tip: '实收材质金额=实收材质重量*材质单价',
      width: 100,
    },
    {
      title: '实收产品金额',
      dataIndex: 'productAmount',
      tip: '实收产品金额=实际签收数量*产品单价',
      width: 85,
    },
    {
      title: '实收工费金额',
      dataIndex: 'laborAmount',
      tip: '实收工费金额=实际签收数量*工费单价',
      width: 85,
    },
    {
      title: '其他费用',
      dataIndex: 'otherAmount',
      tip: '采购交货过程中产生的其他费用',
      width: 85,
    },
    {
      title: '合计金额',
      dataIndex: 'totalAmount',
      tip: '合计金额=实收材质金额+实收产品金额+实收工费金额+其他费用',
      width: 85,
    },
    {
      title: '单件货值',
      dataIndex: 'singleAmount',
      tip: '含税单件货值=合计金额/交货数量--不含税单件货值=（合计金额-税金）/交货数量',
      width: 85,
    },
  ]
  const tableListref = ref()
  const tableLoading = ref(false)
  const tableList = ref<Array<any>>([])
  const expandable = ref<any>({
    title: '',
    defaultExpandAllRows: true,
    expandedRowKeys: [],
    width: 10,
  })
  // 获取商品列表
  async function getTableList() {
    tableList.value = []
    tableListref.value.selectAll(false)
    tableLoading.value = true
    const res: any = await getDeliveryByPurchaseId({ id: props.currentId })
    tableLoading.value = false
    tableList.value = res?.data || []
    expandable.value.expandedRowKeys = tableList.value.map((el: any) => el.id)
  }
  // 合并单元格
  function spanMethodAll(option: any) {
    let { column } = option
    if (column.dataIndex === 'img') {
      return { rowspan: 1, colspan: 17 }
    }
  }
  // 显示sku规格
  function getProductSpecs(item: any) {
    let productSpecs = item.productSpecs ? parseJson(item.productSpecs, []) : []
    return productSpecs.map((el: any) => el.valueCn)?.join('*') || '--'
  }
</script>

<style lang="less" scoped>
  :deep(.arco-table-td) {
    background-color: var(--color-fill-1);
    height: auto;
  }
  :deep(.arco-table-tr-expand) {
    .arco-table-cell {
      padding: 0;
    }
    .arco-table {
      margin: 0 !important;
      .arco-table-td {
        border-bottom: 1px solid var(--color-neutral-3);
        height: 48px;
      }
    }
    background-color: #fff !important;
  }
  :deep(.child-row) {
    .arco-table-cell {
      padding: 2px 8px;
    }
  }
  :deep(.arco-table-tr-expand .arco-table-td) {
    background-color: #fff !important;
    border-bottom: none;
  }
  :deep(.arco-table-tr-expand tr:hover .arco-table-td) {
    background-color: var(--color-fill-1) !important;
  }
  :deep(.arco-table-expand .arco-table-cell) {
    display: none;
  }
  :deep(.arco-tabs-nav-tab) {
    background-color: #fff;
  }
  .order-w {
    color: #666;
    flex: 1;
    width: calc(100vw - @menuWidth - 130px);
    .order-detail-item {
      width: 200px;
      padding-right: 5px;
    }
  }
  .urgent {
    font-size: 12px;
    display: inline-block;
    background-color: red;
    color: #fff;
    width: 18px;
    border-radius: 50%;
    line-height: 18px;
    text-align: center;
    transform: scale(0.82);
  }
</style>
